package com.huilian.iotbox.data.service.impl;

import com.huilian.iotbox.data.enums.ProductFuncDataTypeEnum;
import com.huilian.iotbox.data.enums.ProductIdEnum;
import com.huilian.iotbox.data.po.ProductParamData;
import com.huilian.iotbox.data.service.ProductParamDataCommonService;
import com.huilian.iotbox.data.utils.IotBoxUtil;
import com.huilian.iotbox.data.vo.DeviceVo;
import com.huilian.iotbox.data.vo.ProductParamDataVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;

@Slf4j
@Service
public class ProductParamDataCommonServiceImpl implements ProductParamDataCommonService {

    @Autowired
    private ProductParamDataCommonService productParamDataCommonService;

    /**
     * 默认的基础参数处理
     *
     * @param productParamDataVo
     * @param dataHex
     */
    @Override
    public void defaultMainBoardUnderlyingParam(ProductParamDataVo productParamDataVo, StringBuilder dataHex) {
        String valueHex = null;
        BigDecimal value = BigDecimal.ZERO;
        if (productParamDataVo.getValueType() == 4) {
            // float_0.1(乘10方式)
            value = productParamDataVo.getValue().multiply(BigDecimal.valueOf(10));
        } else if (productParamDataVo.getValueType() == 5) {
            // float_0.01(乘100方式)
            value = productParamDataVo.getValue().multiply(BigDecimal.valueOf(100));
        } else if (productParamDataVo.getValueType() == 6) {
            // float_0.001(乘1000方式)
            value = productParamDataVo.getValue().multiply(BigDecimal.valueOf(1000));
        }
        valueHex = IotBoxUtil.singleInt2Hex_2(value.intValue(), productParamDataVo.getLength());
        if (productParamDataVo.getLength() > 1) {
            valueHex = IotBoxUtil.reverseHex(valueHex);
        }
        log.info("默认处理valueHex:" + valueHex);
        dataHex.append(valueHex);
    }

    /**
     * 永瑞娃娃机基础参数
     *
     * @param productParamDataVo
     * @param dataHex
     */
    @Override
    public void customYongRuiMainBoardUnderlyingParam(ProductParamDataVo productParamDataVo, StringBuilder dataHex) {
        String valueHex = null;
        if (productParamDataVo.getLength() >= 2) {
            // 处理16进制数据超过1字节的需要分割处理，例如10进制6.8的16进制为0608

            // 这里直接使用小数点分割是不行的，所以先替换小数点再分割
            String[] splitArray = String.valueOf(productParamDataVo.getValue()).replace(".", "-").split("-");

            StringBuffer stringBuffer = new StringBuffer(IotBoxUtil.singleInt2Hex(Integer.valueOf(splitArray[0])));
            if (splitArray.length >= 2) {
                stringBuffer.append(IotBoxUtil.singleInt2Hex(Integer.valueOf(splitArray[1])));
            } else {
                stringBuffer.append("00");
            }
            valueHex = stringBuffer.toString();
            log.info("stringBuffer.toString():" + stringBuffer.toString());
        } else {
            // 1字节的
            valueHex = IotBoxUtil.singleInt2Hex_2(productParamDataVo.getValue().intValue(), productParamDataVo.getLength());
        }
        log.info("特殊处理valueHex:" + valueHex);
        dataHex.append(valueHex);
    }

    @Override
    public String defaultMainBoardTerminalParam(ProductParamData productParamDataItem, BigDecimal itemValue) {
        String valueHex = null;
        BigDecimal value = BigDecimal.ZERO;
        if (productParamDataItem.getValueType() == 4) {
            // float_0.1(乘10方式)
            value = itemValue.multiply(BigDecimal.valueOf(10));
        } else if (productParamDataItem.getValueType() == 5) {
            // float_0.01(乘100方式)
            value = itemValue.multiply(BigDecimal.valueOf(100));
        } else if (productParamDataItem.getValueType() == 6) {
            // float_0.001(乘1000方式)
            value = itemValue.multiply(BigDecimal.valueOf(1000));
        }
        valueHex = IotBoxUtil.singleInt2Hex_2(value.intValue(), productParamDataItem.getLength());
        if (productParamDataItem.getLength() > 1) {
            valueHex = IotBoxUtil.reverseHex(valueHex);
        }

        log.info("组装好的16进制参数是：" + valueHex);
        return valueHex;
    }

    /**
     * 颖品娃娃机自定义终端参数
     *
     * @param productParamDataItem
     * @param itemValue
     * @param setFunCode
     * @return
     */
    @Override
    public String customYingPinMainBoardTerminalParam(ProductParamData productParamDataItem, BigDecimal itemValue, Integer setFunCode) {
        if ("67".equals(setFunCode.toString())) {
            // 抓力调节。每个参数对应两个字节，千位百位为小数点前，十位个位为小数点后
            /**
             *
             * 原千位百位和十位个位由16进制转10进制后，用小数点分解
             * 48.0 ————> 48 0
             * 40.0 ————> 40 0
             * 40.0 ————> 40 0
             * 10.4 ————> 10 4
             *
             * 千位百位作为10进制然后转16进制，十位个位也如此
             * 48 0 ————> 30 00
             * 40 0 ————> 28 00
             * 40 0 ————> 28 00
             * 10 4 ————> 0A 04
             *
             * 千位百位 和 十位个位 拼接
             * 30 00 ————> 3000
             * 28 00 ————> 2800
             * 28 00 ————> 2800
             * 0A 04 ————> 0A04
             *
             * 最终按顺序拼接
             * AA0B0242 3000 2800 1000 0A04 4DDD
             * AA0B02423000280010000A044DDD
             *
             */
            String valueHex = null;
            if (productParamDataItem.getLength() >= 2) {
                // 处理16进制数据超过1字节的需要分割处理，例如10进制6.8的16进制为0608
                // 这里直接使用小数点分割是不行的，所以先替换小数点再分割
                String[] splitArray = String.valueOf(itemValue).replace(".", "-").split("-");

                StringBuffer stringBuffer = new StringBuffer(IotBoxUtil.singleInt2Hex(Integer.valueOf(splitArray[0])));
                if (splitArray.length >= 2) {
                    stringBuffer.append(IotBoxUtil.singleInt2Hex(Integer.valueOf(splitArray[1])));
                } else {
                    stringBuffer.append("00");
                }
                valueHex = stringBuffer.toString();
                log.info("stringBuffer.toString():" + stringBuffer.toString());
            } else {
                // 1字节的
                valueHex = IotBoxUtil.singleInt2Hex_2(itemValue.intValue(), productParamDataItem.getLength());
            }
            log.info("特殊处理valueHex:" + valueHex);
            return valueHex;
        } else {
            return this.defaultMainBoardTerminalParam(productParamDataItem, itemValue);
        }
    }

    @Override
    public String getValueHex(ProductParamData productParamData, BigDecimal itemValue, DeviceVo deviceVo, Integer setFunCode) {
        String valueHex = null;
        if (ProductFuncDataTypeEnum.FLOAT_TYPE.getCode().equals(productParamData.getDataType())) {
            log.info("浮点型,itemValue:{}", itemValue);
            // 某些厂家的需要特殊处理
            final ProductIdEnum productIdEnum = ProductIdEnum.getProductId(deviceVo.getDeviceItem().getProductId());
            if (StringUtils.isEmpty(productIdEnum)) {
                valueHex = productParamDataCommonService.defaultMainBoardTerminalParam(productParamData, itemValue);
            } else {
                switch (productIdEnum) {
                    case _103:
                        // 颖品娃娃机
                        valueHex = productParamDataCommonService.customYingPinMainBoardTerminalParam(productParamData, itemValue, setFunCode);
                        break;
                    default:
                        valueHex = productParamDataCommonService.defaultMainBoardTerminalParam(productParamData, itemValue);
                }
            }
        } else {
            log.info("布尔型、整数型、枚举型,itemValue:{}", itemValue);
            valueHex = IotBoxUtil.singleInt2Hex_2(itemValue.intValue(), productParamData.getLength());
            if (productParamData.getLength() > 1) {
                valueHex = IotBoxUtil.reverseHex(valueHex);
            }
        }
        return valueHex;
    }

    @Override
    public void customJiangXinMainBoardUnderlyingParam(ProductParamDataVo productParamDataVo, StringBuilder dataHex) {
        String valueHex = null;
        if (productParamDataVo.getLength() >= 2) {
            // 处理16进制数据超过1字节的需要分割处理，例如10进制6.8的16进制为0608

            // 这里直接使用小数点分割是不行的，所以先替换小数点再分割
            String[] splitArray = String.valueOf(productParamDataVo.getValue()).replace(".", "-").split("-");

            StringBuffer stringBuffer = new StringBuffer(IotBoxUtil.singleInt2Hex(Integer.valueOf(splitArray[0])));
            if (splitArray.length >= 2) {
                stringBuffer.append(IotBoxUtil.singleInt2Hex(Integer.valueOf(splitArray[1])));
            } else {
                stringBuffer.append("00");
            }
            valueHex = stringBuffer.toString();
            log.info("stringBuffer.toString():" + stringBuffer.toString());
        } else {
            // 1字节的
            valueHex = IotBoxUtil.singleInt2Hex_2(productParamDataVo.getValue().intValue(), productParamDataVo.getLength());
        }
        log.info("特殊处理valueHex:" + valueHex);
        dataHex.append(valueHex);
    }

    public static void main(String[] args) {
        ProductParamDataVo productParamDataVo = new ProductParamDataVo();
        productParamDataVo.setLength(2);
        productParamDataVo.setValue(BigDecimal.valueOf(39.9));


    }
}
